export function eachNode(
  rootNode: Node,
  callback?: (element: HTMLElement) => boolean | void
): HTMLElement[] | boolean | void {
  if (rootNode.nodeType !== Node.ELEMENT_NODE) {
    return;
  }

  if (!callback) {
    const nodes: HTMLElement[] = [];
    eachNode(rootNode, function (node) {
      nodes.push(node);
    });
    return nodes;
  }

  if (rootNode.hasChildNodes()) {
    const nodes = rootNode.childNodes;
    for (let i = 0, l = nodes.length; i < l; ++i) {
      if (false === eachNode(nodes[i], callback)) {
        return;
      }
    }
  }

  if (false === callback(rootNode as HTMLElement)) {
    return false;
  }
}
